package com.zmj.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.zmj.amc.sdk.entity.User;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zmj.framework.base.service.impl.BaseServiceImpl;
import com.zmj.framework.exception.CustomException;
import com.zmj.framework.response.ResponseCode;
import com.zmj.framework.util.ThreadContextHandler;
import com.zmj.mobile.dto.AuditDTO;
import com.zmj.mobile.dto.SealApplyDTO;
import com.zmj.mobile.entity.OrgEntity;
import com.zmj.mobile.entity.SealApplyEntity;
import com.zmj.mobile.listener.SealApplyExtraListener;
import com.zmj.mobile.mapper.ISealApplyMapper;
import com.zmj.mobile.mapstruct.SealApplyMapStruct;
import com.zmj.mobile.process.AuditServiceEnum;
import com.zmj.mobile.process.AuditServiceFactory;
import com.zmj.mobile.process.IAuditService;
import com.zmj.mobile.service.IOrgService;
import com.zmj.mobile.service.ISealApplyService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * <p>
 * 用印申请表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2024-02-01
 */
@Service
public class SealApplyServiceImpl extends BaseServiceImpl<ISealApplyMapper, SealApplyEntity> implements ISealApplyService {
    @Resource
    private AuditServiceFactory auditServiceFactory;

    @Resource
    private IOrgService iOrgService;

    @Override
    public void insert(SealApplyDTO sealApplyDTO) {
        User user = new User();
        user.setUsername(sealApplyDTO.getCreateUser());
        user.setRealName(sealApplyDTO.getCreateUserName());
        ThreadContextHandler.setCurrentUser(user);

        Optional<OrgEntity> orgEntityOptional = iOrgService.getByUserName(sealApplyDTO.getCreateUser());
        if (orgEntityOptional.isPresent()) {
            sealApplyDTO.setDepartCode(orgEntityOptional.get().getOrgCode());
            sealApplyDTO.setDepartName(orgEntityOptional.get().getOrgName());
        }

        SealApplyEntity entity = SealApplyMapStruct.INSTANCE.toEntity(sealApplyDTO);

        baseMapper.insert(entity);
        IAuditService auditService = auditServiceFactory.getAuditService(AuditServiceEnum.getExample(AuditServiceEnum.SEAL_APPLY.getCode()));
        auditService.agree(AuditDTO.builder().id(entity.getId()).build());
    }

    @Override
    public SealApplyEntity detail(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * @Description 导入用印申请
     * @Author ChengShenzhen
     * @Date 18:18 2024/2/1 0001
     **/
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void importExcel(MultipartFile file,int sheet){

        //加载excel文件
        List<SealApplyDTO> sealApplyDTOS = EasyExcel.read(file.getInputStream(), SealApplyDTO.class, new SealApplyExtraListener()).excelType(ExcelTypeEnum.XLSX)
             .extraRead(CellExtraTypeEnum.HYPERLINK).extraRead(CellExtraTypeEnum.HYPERLINK)
             .head(SealApplyDTO.class)
             .sheet(sheet)
             .doReadSync();
        if (CollectionUtil.isEmpty(sealApplyDTOS)) {
            throw new CustomException(ResponseCode.INTERNAL_SERVER_ERROR.getCode());
        }
        //用印申请集合数据
        List<SealApplyEntity> sealApplyEntityList = new ArrayList<>();
        for (SealApplyDTO sealApplyDTO:sealApplyDTOS){
            SealApplyEntity sealApplyEntity = new SealApplyEntity();
            BeanUtil.copyProperties(sealApplyDTO, sealApplyEntity);
            sealApplyEntityList.add(sealApplyEntity);
        }
        this.saveBatch(sealApplyEntityList);
    }
}
